package com.study.util;

import lombok.extern.slf4j.Slf4j;

import java.io.*;

import java.util.ArrayList;
import java.util.List;


/**
 * @version V1.0
 * @title: Util
 * @description: ${DESCRIPTION}
 * @author: wanghuilong
 * @date: 2019-05-21 14:36
 * @copyright: http://fingo.com.my/
 */
@Slf4j
public class FileUtil {
    public static List<String> readTxtFileIntoStringArrList(String filePath,
                                                            String encoding) {
        log.info("addLogStart readTxtFileIntoStringArrList InputParam: filePath : {} encoding : {} ",
                filePath, encoding);

        List<String> list = new ArrayList<String>();

        try {
            encoding = (encoding == null) ? "UTF-8" : "GBK";

            File file = new File(filePath);

            if (file.isFile() && file.exists()) { // 判断文件是否存在

                InputStreamReader read = new InputStreamReader(new FileInputStream(
                        file), encoding); // 考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;

                while ((lineTxt = bufferedReader.readLine()) != null) {
                    list.add(lineTxt);
                }

                bufferedReader.close();
                read.close();
            } else {
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }

        return list;
    }

    public static String read(String path) {
        log.info("addLogStart read InputParam: path : {} ", path);

        StringBuffer stringBuffer = new StringBuffer();

        try {
            readToBuffer(stringBuffer, path);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //System.out.println(stringBuffer);
        return stringBuffer.toString();
    }

    /**
     * 将文本文件中的内容读入到buffer中
     *
     * @param buffer   buffer
     * @param filePath 文件路径
     * @throws IOException 异常
     * @author cn.outofmemory
     * @date 2013-1-7
     */
    public static void readToBuffer(StringBuffer buffer, String filePath)
            throws IOException {

        InputStream is = new FileInputStream(filePath);
        String line; // 用来保存每行读取的内容
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        line = reader.readLine(); // 读取第一行

        while (line != null) { // 如果 line 为空说明读完了
            buffer.append(line); // 将读到的内容添加到 buffer 中
            buffer.append("\n"); // 添加换行符
            line = reader.readLine(); // 读取下一行
        }

        reader.close();
        is.close();
    }

    public static void writeToFileByLine(List<String> sourceLine,
                                         String filePath) {

        //写入相应的文件
        OutputStreamWriter out = null;

        try {
            out = new OutputStreamWriter(new FileOutputStream(filePath));

            for (int i = 0; i < sourceLine.size(); i++) {
                String line = sourceLine.get(i);

                if (null != line) {
                    if (!line.trim().endsWith("\n")) {
                        line += "\n";
                    }
                }

                out.write(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != out) {
                try {
                    out.flush();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void writeToFileOverWrite(String source, String filePath) {

        //写入相应的文件
        OutputStreamWriter out = null;

        try {
            out = new OutputStreamWriter(new FileOutputStream(filePath));
            out.write(source);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != out) {
                try {
                    out.flush();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void writeToFileOverAppend(String source, String filePath) {

        //写入相应的文件
        OutputStreamWriter out = null;

        try {
            out = new OutputStreamWriter(new FileOutputStream(filePath,true));
            out.write(source);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != out) {
                try {
                    out.flush();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static List<File> getAllFile(File dir) {
        List<File> fileList = new ArrayList<File>();
        if (null == dir || !dir.exists() || !dir.isDirectory()) {
            return fileList;
        }
        File[] files = dir.listFiles();
        for (int i = 0; i < files.length; i++) {
            File subFile = files[i];
            if (subFile.isFile()) {
                fileList.add(subFile);
            } else if (subFile.isDirectory()) {
                fileList.addAll(getAllFile(subFile));
            }
        }

        return fileList;
    }
}
